$(document).ready(function() {
    function pageLoading() {
        setTimeout(function() {
            var index = layer.load(2, {
                shade: [0.1, '#fff']
            });
            setTimeout(function() {
                layer.close(index);
                $('.index').css({
                    'opacity': '1'
                })
            }, 500)
        }, 100)
    }
    pageLoading();
    var index = layer.load(0, {
        shade: false
    });
    $('.signInBtn').on('click', function() {
        var phone = $('#phone').val();
        var password = $('.cellpwd input').val();
        var pwd = $.md5(password);
        var str = {
            mobile_phone: phone,
            pwd: pwd
        };
        var index;
        console.log(str);
        $.ajax({
            type: "POST",
            url: '/platform/login',
            data: JSON.stringify(str),
            headers: {
                "content-type": "application/json"
            },
            beforeSend: function(xhr) {
                index = layer.load(0, {
                    shade: false
                });
            },
            success: function(json) {
                layer.close(index);
                if (json.code == 0) {
                    layer.msg('登录成功', {
                        time: 1000
                    });
                    var userInfo = JSON.stringify(json.content);
                    removeCookie('userInfo');
                    setCookie('userInfo', userInfo, 1);
                    var userInfo = getCookie('userInfo');
                    var jsObject = JSON.parse(userInfo);
                    var time = Date.parse(new Date());
                    var User = String(jsObject.pk);
                    var Secret1 = 'WOCAONIMAGEBIgannilao123';
                    var Secret2 = 'xxx';
                    var str = User + time + Secret1;
                    var pwd = $.md5(str);
                    pwd = pwd.toUpperCase();
                    var str2 = pwd + Secret2;
                    var md5 = $.md5(str2);
                    md5 = md5.toUpperCase();
                    jsObject.auth = md5;
                    jsObject.time = time;
                    var userInfo = JSON.stringify(jsObject);
                    setCookie('userInfo', userInfo, 1);
                    setTimeout(function() {
                        window.location.href = "/profileSkip?pk=" + User + "&time=" + time + "&auth=" + md5
                    }, 100)
                } else {
                    console.log(json);
                    layer.msg(json.message)
                }
            },
            error: function(textStatus) {
                console.error(textStatus)
            },
            complete: function(XMLHttpRequest, status) {
                if (status == 'timeout') {
                    xhr.abort();
                    layer.msg("网络超时，请刷新", function() {
                        location.reload()
                    })
                }
            }
        })
    });
    $('.exitUser').on('click', function() {
        removeCookie("userInfo");
        window.location.href = "signin2Skip"
    });
    $('.registerBtn').on('click', function() {
        var phone = $("#cellPhone").val();
        var password = $('.password input').val();
        var str = {
            "mobile_phone": phone,
            "pwd": '03776050'
        };
        console.log('+++'+JSON.stringify(str));
        var time = Date.parse(new Date());
        var index;
        $.ajax({
            type: "POST",
            url: '/platform/user?user=cos&time='+time+'&auth=auth',
            data: str,
            headers: {
                "content-type": "application/json"
            },
            beforeSend: function(xhr) {
                console.log(xhr);
                index = layer.load(0, {
                    shade: false
                });
            },
            success: function(json) {
                layer.close(index);
                if (json.code == 0) {
                    layer.msg('注册成功', {
                        time: 2000
                    });
                    console.log(json);
                    var userInfo = JSON.stringify(json.content);
                    removeCookie('userInfo');
                    setCookie('userInfo', userInfo, 1);
                    var userInfo = getCookie('userInfo');
                    var jsObject = JSON.parse(userInfo);
                    var time = Date.parse(new Date());
                    var User = String(jsObject.pk);
                    var Secret1 = 'WOCAONIMAGEBIgannilao123';
                    var Secret2 = 'xxx';
                    var str = User + time + Secret1;
                    var pwd = $.md5(str);
                    pwd = pwd.toUpperCase();
                    var str2 = pwd + Secret2;
                    var md5 = $.md5(str2);
                    md5 = md5.toUpperCase();
                    jsObject.auth = md5;
                    jsObject.time = time;
                    var userInfo = JSON.stringify(jsObject);
                    setCookie('userInfo', userInfo, 1);
                    setTimeout(function() {
                        window.location.href = "/profileSkip?pk=" + User + "&time=" + time + "&auth=" + md5
                    }, 100)
                } else {
                    layer.msg(json.message)
                }
            },
            error: function(textStatus) {
                console.error(textStatus)
            },
            complete: function(XMLHttpRequest, status) {
                console.log(XMLHttpRequest);
                if (status == 'timeout') {
                    xhr.abort();
                    layer.msg("网络超时，请刷新", function() {
                        location.reload()
                    })
                }
            }
        })
    });
    $('footer').on('click', '.f-wallet', function() {
        var userID = getQueryString('user');
        $.ajax({
            url: "/promote/user/" + userID,
            type: "GET",
            dataType: "json",
            success: function(result) {
                console.log(result);
                if (result.code == 0) {
                    $('.feedback-wrap, .feedback-box').fadeIn()
                } else if (result.code == 1) {
                    layer.msg('您已提交过钱包地址！', {
                        time: 2000
                    })
                }
            }
        })
    })
});

function setCookie(key, value, iDay) {
    var oDate = new Date();
    oDate.setDate(oDate.getDate() + iDay);
    document.cookie = key + '=' + value + ';expires=' + oDate
}

function removeCookie(key) {
    setCookie(key, '', -1);
}

function getCookie(key) {
    var cookieArr = document.cookie.split('; ');
    for (var i = 0; i < cookieArr.length; i++) {
        var arr = cookieArr[i].split('=');
        if (arr[0] === key) {
            return arr[1]
        }
    }
    return false
}